﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.Generic;

namespace MLY.Bll
{
    /// <summary>
    /// Summary description for clsUser
    /// </summary>
    [DataObject]
    public class clsUser
    {
        public enum enumRoleId
        {
            None = -1,
            Role10_Operation = 10,
            Role20_Management = 20,
            Role30_IT = 30,
            Role40_CustomerAdmin = 40,
            Role60_Admin = 60
        }

        #region Constructors
        // ==================================
        public clsUser()
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableUser();
            IsExist = false;
        }

        public clsUser(string Username)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableUsers
                        where (o.UserName == Username)
                        select o).FirstOrDefault();
            IsExist = (_DataRow != null);

        }



        public clsUser(int Id)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableUsers
                        where (o.UserId == Id)
                        select o).FirstOrDefault();

            IsExist = (_DataRow != null);

        }
        // ==================================
        #endregion

        #region Public Methods
        // ==================================
        public void Save()
        {
            if (IsExist)
            {
                _db.SubmitChanges();
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;

            }
            else
            {
                _DataRow.DateCreated = DateTime.Now;
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdateBy = clsUser.CurrentUserId;

                _db.TableUsers.InsertOnSubmit(_DataRow);
                _db.SubmitChanges();
            }
        }
        public bool CheckPassword(string Password)
        {
            return (this.IsExist && this.Data.Password == Password);
        }
        public void SetPassword(string Password)
        {
            this.Data.Password = Password;
        }
        public bool DeleteUser(int UserId)
        {
            if (Bll.clsUser.CurrentUserId != UserId)
            {
                _db = new Dal.lqKbmDataContext(g.AppConnectionString);
                _DataRow = (from o in _db.TableUsers
                            where (o.UserId == UserId)
                            select o).First();
                _db.TableUsers.DeleteOnSubmit(_DataRow);
                _db.SubmitChanges();
                return true;
            }
            return false;
        }

        public bool isAdmin()
        {
            if (HttpContext.Current.Session["RoleId"] == null)
                return false;
            return (int)HttpContext.Current.Session["RoleId"] == 60;
        }

        // ==================================
        #endregion

        #region Static Members
        // ==================================
        public static int CurrentUserId
        {
            get
            {
                System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
                return (session["UserId"] != null ? (int)session["UserId"] : -1);
            }
        }
        public static string CurrentUserFullName
        {
            get
            {
                System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
                return (session["FullName"] != null ? (string)session["FullName"] : "");
            }
        }
        public static string CurrentUserCompanyName
        {
            get
            {
                System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
                return (session["CompanyName"] != null ? (string)session["CompanyName"] : "");
            }
        }
        public static int CurrentUserRoleId
        {
            get
            {
                System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
                return (session["RoleId"] != null ? (int)session["RoleId"] : -1);
            }
        }

        public static int CurrentUserOrganizationId
        {
            get
            {
                System.Web.SessionState.HttpSessionState session = HttpContext.Current.Session;
                return (session["OrganizationId"] != null ? (int)session["OrganizationId"] : -1);
            }
        }


        public static bool IsUsernameFree(string Username, int UserId)
        {
            using (Dal.lqKbmDataContext _db = new MLY.Dal.lqKbmDataContext())
            {
                var user = (from o in _db.TableUsers
                            where ((o.UserName == Username) && (o.UserId != UserId))
                            select o).FirstOrDefault();
                return (user == null);
            }
        }

        public List<Dal.TableCsvTemplateForUser> getCsvHeaders(int tempateId)
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                var x = (from o in db.TableCsvTemplateForUsers
                         join q in db.Lookup_CsvFields
                         on o.CsvTemplate_FieldId equals q.Idx
                         join t in db.TableCsvTemplates
                         on o.templateId equals t.idx
                         where t.TableUser.UserId == Data.UserId
                         && o.templateId == tempateId
                         select o).ToList();
                return x;
            }
        }

        public static List<Dal.TableUser> GetUsers()
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                return (from o in db.TableUsers select o).ToList<Dal.TableUser>();
            }
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableUser Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableUser _DataRow = null;
        // ==================================
        #endregion
    }
}